package com.hm.common.su;

import java.io.Serializable;

/**
 * @author shishun.wang
 * @version 1.0
 * @date 2018/8/31 17:46
 * @describe
 */
public class ServerResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String OK = "ok";

    private static final String ERROR = "error";

    private T data;

    private MetaType metaType;

    public ServerResponse<T> success() {
        this.metaType = new MetaType(true, OK);
        return this;
    }

    public ServerResponse<T> success(T data) {
        this.metaType = new MetaType(true, OK);
        this.data = data;
        return this;
    }

    public ServerResponse<T> failure() {
        this.metaType = new MetaType(false, ERROR);
        return this;
    }

    public ServerResponse<T> failure(String message) {
        this.metaType = new MetaType(false, message);
        this.data = null;
        return this;
    }

    public ServerResponse<T> failure(String code, String message) {
        this.metaType = new MetaType(false, code, message);
        this.data = null;
        return this;
    }

    public T getData() {
        return data;
    }

    public MetaType getMetaType() {
        return metaType;
    }

    public static class MetaType implements Serializable {

        private static final long serialVersionUID = 5227261674159084436L;

        private boolean success;

        private String code;

        private String message;

        public MetaType() {
        }

        public MetaType(boolean success, String message) {
            this.success = success;
            this.message = message;
        }

        public MetaType(boolean success, String code, String message) {
            this.code = code;
            this.success = success;
            this.message = message;
        }

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

    }

    public static String getOk() {
        return OK;
    }

    public static String getError() {
        return ERROR;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setMetaType(MetaType metaType) {
        this.metaType = metaType;
    }
}